package com.anplurk.model;

import java.net.URI;
import java.util.Comparator;

import org.json.JSONException;
import org.json.JSONObject;


    public class UserModel implements Comparator<UserModel> {
        
        String uid;
        String avatarSrc;                
        String nickname;        
        String password;
        String defaultLang;
        String displayName;
        
        public static final String LANG_EN="en";
        public static final String LANG_TC="tr_ch";
         
        public String toString(){            
            return "uid="+uid + ", nickname=" + nickname + ", password=" + password + ",displayName="+ displayName +", avatarSrc=" +avatarSrc + ",default lang=" + defaultLang;                      
        }
        
        public static UserModel newInstance(JSONObject obj) throws JSONException {
            UserModel user = new UserModel();
            
            String uid = obj.getString("uid");
            user.setUid(uid);
            user.setNickname(obj.getString("nick_name"));
            String avatar = obj.getString("avatar");
            if (!avatar.equals("null")){
                user.setAvatarSrc("http://avatars.plurk.com/" + uid + "-medium"+ avatar  + ".gif");
            }
            else {
                user.setAvatarSrc("http://avatars.plurk.com/" + uid + "-medium.gif");
            }
            
            user.setDisplayName(obj.getString("display_name"));
            user.setDefaultLang(obj.isNull("default_lang")?UserModel.LANG_TC:obj.getString("default_lang"));            
            return user;    
        }
        
        
        public void setDefaultLang(String lang){
            if (lang.equals(UserModel.LANG_EN) | lang.equals(UserModel.LANG_TC) ){
                this.defaultLang = lang;
            }  
            else {
                defaultLang = UserModel.LANG_TC;
            }
        }
        
        public String getDefaultLang(){
            return this.defaultLang;
        }
        
        
        public String getAvatarSrc() {
            return avatarSrc;
        }
        
        public URI getAvatarUri(){
            return URI.create(this.getAvatarSrc());
        } 
        
        public void setAvatarSrc(String avatarSrc) {
            this.avatarSrc = avatarSrc;
        }
        public String getNickname() {
            return nickname;
        }
        public void setNickname(String nickname) {
            this.nickname = nickname;
        }
        
        public String getUid() {
            return uid;
        }
        
        public void setUid(String uid) {
            this.uid = uid;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
        public String getDisplayName() {
            return displayName;
        }

        public void setDisplayName(String displayName) {
            this.displayName = displayName;
        }

        public int compare(UserModel o1, UserModel o2) {
            return o1.getUid().compareTo(o2.getUid());            
        }
        
        public String getName(){
            String name = this.getDisplayName();
            return (name == null || name.equals("") || name.equals("null"))?this.getNickname():name;  
        }
    }
